package Cau13;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

import javax.swing.JButton;
import java.awt.Color;


public class ClientRegister implements ActionListener {

	private JFrame frmClientregistersinhcoi;
	private JTextField txtuser;
	private JPasswordField txtpass;
	JButton btnRegister;
	JButton btnLogin;
	private JLabel lbkq;
	private JTextField txthot;
	private JLabel lblAddServer;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ClientRegister window = new ClientRegister();
					window.frmClientregistersinhcoi.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public ClientRegister() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmClientregistersinhcoi = new JFrame();
		frmClientregistersinhcoi.setTitle("CLientRegister_Sinh_Coi");
		frmClientregistersinhcoi.setBounds(100, 100, 423, 300);
		frmClientregistersinhcoi.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmClientregistersinhcoi.getContentPane().setLayout(null);
		
		txtuser = new JTextField();
		txtuser.setBounds(151, 90, 133, 20);
		frmClientregistersinhcoi.getContentPane().add(txtuser);
		txtuser.setColumns(10);
		
		txtpass = new JPasswordField();
		txtpass.setBounds(151, 122, 133, 20);
		frmClientregistersinhcoi.getContentPane().add(txtpass);
		
		JLabel lblUserName = new JLabel("User Name:");
		lblUserName.setBounds(72, 93, 69, 14);
		frmClientregistersinhcoi.getContentPane().add(lblUserName);
		
		JLabel lblPassword = new JLabel("PassWord:");
		lblPassword.setBounds(72, 125, 69, 14);
		frmClientregistersinhcoi.getContentPane().add(lblPassword);
		
		JLabel lblLoginAndRegister = new JLabel("Login and Register");
		lblLoginAndRegister.setFont(new Font("Tahoma", Font.PLAIN, 17));
		lblLoginAndRegister.setHorizontalAlignment(SwingConstants.CENTER);
		lblLoginAndRegister.setBounds(88, 31, 211, 34);
		frmClientregistersinhcoi.getContentPane().add(lblLoginAndRegister);

		btnLogin = new JButton("Login");
		btnLogin.setBounds(88, 164, 89, 23);
		btnLogin.addActionListener(this);
		frmClientregistersinhcoi.getContentPane().add(btnLogin);
		
		btnRegister = new JButton("Register");
		btnRegister.setBounds(208, 164, 89, 23);
		btnRegister.addActionListener(this);
		frmClientregistersinhcoi.getContentPane().add(btnRegister);
		
		lbkq = new JLabel("");
		lbkq.setForeground(Color.MAGENTA);
		lbkq.setHorizontalAlignment(SwingConstants.CENTER);
		lbkq.setFont(new Font("Tahoma", Font.PLAIN, 16));
		lbkq.setBounds(50, 201, 324, 34);
		frmClientregistersinhcoi.getContentPane().add(lbkq);
		
		txthot = new JTextField();
		txthot.setText("localhost");
		txthot.setBounds(151, 61, 133, 20);
		frmClientregistersinhcoi.getContentPane().add(txthot);
		txthot.setColumns(10);
		
		lblAddServer = new JLabel("Add Server");
		lblAddServer.setBounds(72, 64, 69, 14);
		frmClientregistersinhcoi.getContentPane().add(lblAddServer);
	}

	@Override
	public void actionPerformed(ActionEvent e0) {
		String ip = txthot.getText().trim();
		if(ip.equals("") || txtpass.getText().equals("") || txtuser.getText().equals(""))
			{
				JOptionPane.showMessageDialog(null, "Enter the text boxes");
				txtuser.setFocusable(true);
				return;
			}
		if(e0.getSource()==btnLogin)
		{
			
			/////
			try {
				
				Iregister rmiObj = (Iregister)Naming.lookup("rmi://"+ip+":1099/Serverregister");
				Account acc = new Account(txtuser.getText().trim(), txtpass.getText().trim());
				if(rmiObj.signIn(acc))
					lbkq.setText("Login successful");
				else lbkq.setText("Login failed");			
				
			} catch (MalformedURLException e) {
				lbkq.setText("Server address is invalid");
				e.printStackTrace();
			} catch (RemoteException e) {
				lbkq.setText("Server not found");
				e.printStackTrace();
			} catch (NotBoundException e) {
				lbkq.setText("Server not found");
				e.printStackTrace();
			}
		}
		if(e0.getSource()==btnRegister)
		{
			try {
				
				Iregister rmiObj = (Iregister)Naming.lookup("rmi://"+ip+":1099/Serverregister");
				Account acc = new Account(txtuser.getText().trim(), txtpass.getText().trim());
				if(rmiObj.register(acc))
					lbkq.setText("Register successful");
				else lbkq.setText("Username is existed");
				
				
				
			} catch (MalformedURLException e) {
				lbkq.setText("Server address is invalid");
				e.printStackTrace();
			} catch (RemoteException e) {
				lbkq.setText("Server not found");
				e.printStackTrace();
			} catch (NotBoundException e) {
				lbkq.setText("Server not found");
				e.printStackTrace();
			}
		}
		
	}
	
}
